<?
use adminmatrix\migration\Migrator;
use  adminmatrix\migration\db\Column;

class Setting extends Migrator
{
    /**
         * Change Method.
         *
         * Write your reversible migrations using this method.
         *
         * More information on writing migrations is available here:
         * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
         *
         * The following commands can be used in this method and Phinx will
         * automatically reverse them when rolling back:
         *
         *    createTable
         *    renameTable
         *    addColumn
         *    renameColumn
         *    addIndex
         *    addForeignKey
         *
         * Remember to call "create()" or "update()" and NOT "save()" when working
         * with the Table class.
         */
    public function up()
    {
        $this->createAdminSetting();

        $this->createAdminSettingConfig();
    }

    public function down()
    {
        $this->table('admin_setting')->drop()->save();
        $this->table('admin_setting_config') ->drop()->save();
    }

    private function createAdminSetting()  {
        $table = $this->table('admin_setting' ,[
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '系统配置标题',
            'id' => 'id',
            'primary_key' => ['id']
        ]);

        $table->addColumn('name' , 'string' , [
            'null'  =>  false,
            'limit' =>  64,
            'comment'   =>  '配置唯一标识'
        ]);

        $table->addColumn('title' , 'string' , [
            'null'  =>  false,
            'limit' =>  64,
            'comment'   =>  '标题'
        ]);

        $table->addIndex('name',[
            'name'  =>  'name',
            'unique'    =>  'true'
        ]);

        $table->create();
    }


    private function createAdminSettingConfig(){
        $table = $this->table('admin_setting_config' , [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '系统配置项',
            'id' => 'id',
            'primary_key' => ['id']
        ]);

        $table->addColumn('setting_id' , 'integer' , [
            'null'   =>  false,
            'default'    =>  0,
            'comment'    =>  '设置id',
        ]);


        $table->addColumn('name' , 'string' , [
            'null'   =>  false,
            'limit'     =>  128,
//            'default'    =>  '',
            'comment'    =>  '唯一标识',
        ]);

        $table->addColumn('type' , 'char' , [
            'null'  =>true,
            'limit' =>  32,
            'default'   =>  'text',
            'comment'   =>  '字段类型'
        ]);

        $table->addColumn('value' , 'text' , [
            'null'  =>  true,
            'comment'   =>  '数据内容'
        ]);

        $table->addColumn('create_time' , 'integer' ,[
            'null'  =>  false,
            'limit' =>  11,
            'comment'   => '创建时间'
        ]);

        $table->addColumn('update_time' , 'integer' ,[
            'null'  =>  true,
            'limit' =>  11,
            'comment'   => '编辑时间'
        ]);

        $table->addColumn('delete_time' , 'integer' ,[
            'null'  =>  true,
            'limit' =>  11,
            'comment'   => '删除时间'
        ]);

        $table->addIndex('name' , [
            'name'  =>  'name',
            'unique'    =>  'true'
        ]);
        $table->addIndex('setting_id',[
            'name'  =>  'setting_id',
            'unique'    =>false
        ]);

        $table->create();
    }

}